package com.AAL.DAO;

import java.sql.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IPurchaseContractDAO;
import com.AAL.Model.PurchaseContract;
import com.AAL.Model.StatusOfContract;
import com.AAL.Utils.HibernateUtil;
import com.sun.jmx.snmp.Timestamp;

public class PurchaseContractDAO implements IPurchaseContractDAO {

	@Override
	public PurchaseContract saveAndUpdatePurchaseContract(
			PurchaseContract PurchaseContract) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(PurchaseContract);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return PurchaseContract;
	}

	@Override
	public PurchaseContract getPurchaseContract(int purchaseContractID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		PurchaseContract purchaseContract = (PurchaseContract) session.load(
				PurchaseContract.class, purchaseContractID);
		return purchaseContract;
	}

	// Written by Thuong
	// Get List PurchaseInvoice by PurchaseConstractID
	public List<PurchaseContract> getLstPurchaseContractByPurchaseContractID(
			int purchaseContractID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		List<PurchaseContract> purchaseContract = null;
		try {
			session.beginTransaction();
			purchaseContract = (List<PurchaseContract>) session
					.createQuery(
							"from PurchaseContract where purchaseContract.purchaseContractID=:purchaseID")
					.setParameter("purchaseID", purchaseContractID).list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}

		return purchaseContract;
	}

	// get list all purchaseContract
	public List<PurchaseContract> getLstAllPurchaseContract() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		List<PurchaseContract> purchaseContract = null;
		try {
			session.beginTransaction();
			purchaseContract = (List<PurchaseContract>) session.createQuery(
					"from PurchaseContract").list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
		return purchaseContract;
	}

	@SuppressWarnings("unchecked")
	public List<PurchaseContract> getLstPurchaseContractByStatus(
			StatusOfContract status) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		List<PurchaseContract> purchaseContract = null;
		try {
			session.beginTransaction();
			purchaseContract = (List<PurchaseContract>) session
					.createQuery(
							"from PurchaseContract where purchaseContract.statusOfContract.statusOfContractID= :status")
					.setParameter("status", status.getStatusOfContractID())
					.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		return purchaseContract;

	}
	public List<PurchaseContract> getLstPurchaseContractsByWritingDate(
			Timestamp fromDate, Timestamp toDate) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		List<PurchaseContract> purchaseContract = null;
		try {
			session.beginTransaction();
			purchaseContract = (List<PurchaseContract>) session
					.createQuery(
							"from PurchaseContract where atTime between :begin and :end")
					.setParameter("begin", fromDate)
					.setParameter("end", toDate)
					.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		return purchaseContract;
	}
	@Override
	public PurchaseContract deletePurchaseContract(int purchaseContractID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			PurchaseContract purchaseContract = (PurchaseContract) session
					.load(PurchaseContract.class, purchaseContractID);
			if (null != purchaseContract) {
				session.delete(purchaseContract);
			}
			session.getTransaction().commit();
			return purchaseContract;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	}

	// The end Thuong

	@Override
	public List<PurchaseContract> getLstPurchaseContractsByWritingDateAndStatus(
			StatusOfContract status, Date fromDate, Date toDate) {
		// TODO Auto-generated method stub
		return null;
	}

}
